<?php
namespace application\views;
use application\system\Gandalf;
class Admin extends \application\auth\Admin {
	protected $Panel;
	public $messages = array();
	public $handleErrors = TRUE;
	public function __construct() {
		global $concerto;
		$display = $_REQUEST['display'];
		$type = $_REQUEST['type'];
		
		
		if($display) {
			$panelClass = 'application\\admin\\panels\\'.$display;
			if(class_exists($panelClass)) {
				$this->Panel = new $panelClass;
				
			}
			elseif(Modulator()->panelExists($display)) {
				Modulator()->loadPanel($display);
				$this->Panel = new $display;
			}
		}
		else {
			$this->Panel = new \application\admin\panels\home;
		}
		
		
	}
	
	public function load() {
		global $concerto;
		require CN_APPLICATION_PATH.'application/functions/admin.php';
		require CN_APPLICATION_PATH.'application/functions/front-end.php';
		
		
		
		if(getQueryVar('mode') == 'ajax-content') {
			$this->content();
			exit();
		}
		if(getQueryVar('mode') == 'ajax-title') {
			echo $this->Panel->title();
			exit();
		}
		elseif(getQueryVar('mode') == 'ajax-method') {
			$method = getQueryVar('method');
			$this->Panel->$method();
			exit();
		}
		elseif(getQueryVar('mode') == 'json-sidebar') {
			echo json_encode($this->Panel->sidebar());
			exit();
		}
		elseif(getQueryVar('mode') == 'html-sidebar') {
			$this->Panel->sidebar();
			exit();
		}
		elseif(getQueryVar('mode') == 'handle-data') {
			$this->Panel->handleData();
		}
		else {
			// Update the database if there's a new version
			$userVersion = getOption('current_version');
			$appVersion = file_get_contents(CN_APPLICATION_PATH.'application/version.txt');
			if($appVersion > $userVersion) {
				require CN_APPLICATION_PATH.'application/utilities/update.php';
			}
			
			$this->Panel->handleData();
			Gandalf::generateToken();
			$this->header();
			
			$this->footer();
		}
		
		
		
	}
	
	protected function header() {
		include CN_APPLICATION_PATH . 'application/admin/template/header.php';
	}
	
	protected function sidebar() {
		include CN_APPLICATION_PATH . 'application/admin/template/sidebar.php';
	}
	
	protected function content() {
		$this->Panel->display();
	}
	
	protected function footer() {
		include CN_APPLICATION_PATH . 'application/admin/template/footer.php';
	
	}
		
}

?>